package com.sort;

import java.util.Arrays;

/**
 * 选择排序
 * 最好 n^2
 * 最坏 n^2
 * 平均 n^2
 */
public class SelectionSorter implements Sort {
    @Override
    public void sort(int[] arr) {
        if (arr == null || arr.length <= 1) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            int min = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[min]) {
                    min = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[min];
            arr[min] = temp;
        }
    }


    public static void main(String[] args) {
        int[] array = SortUtils.buildRandomIntArray(10);
        SortUtils.printArray(array);
        int[] arrayCopy = Arrays.copyOf(array, array.length);
        Sort sort = new SelectionSorter();
        sort.sort(array);
        SortUtils.printArray(array);
        if (SortUtils.isEquals(array, arrayCopy))
            System.out.println("The two array is not equal ... ");
        else
            System.out.println("The two array is equal ... ");
    }
}
